package org.apache.ibatis.demo.test;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.demo.dao.BlogMapper;
import org.checkerframework.checker.units.qual.A;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Date 2021/3/8 23:06
 * @Created mobian
 * @Description 手写MyBatis，获取注解头上的sql语句
 */
public class Demo06 {
  public static void main(String[] args) {
//    SqlSession sqlSession = new SqlSession();
//    BlogMapper mapper = (BlogMapper) sqlSession.getMapper(BlogMapper.class);

    MapperProxy mapperProxy = new MapperProxy();
    mapperProxy.setTarget(BlogMapper.class);
    BlogMapper mapper = (BlogMapper) mapperProxy.getProxy(BlogMapper.class);

    mapper.queryBlogList("32");

  }
}


class SqlSession {
  public Object getMapper(Class clazz){
    return Proxy.newProxyInstance(clazz.getClassLoader(),new Class[]{clazz},new MapperProxy());
  }
}

class MapperProxy implements  InvocationHandler{

  private  Object target;

  public  Object getTarget() {
    return target;
  }

  public  void setTarget(Object target) {
    this.target = target;
  }

  public Object getProxy(Class clazz){
    return Proxy.newProxyInstance(clazz.getClassLoader(),new Class[]{clazz},new MapperProxy());
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {



    Select annotation = method.getAnnotation(Select.class);
    String[] value = annotation.value();
    System.out.println(value[0]);

    Object invoke = method.invoke(target, args);
    return invoke;
  }
}

